using System;
using System.Collections.Generic;
using System.Text;
using DigitalFortress.Core;
using Castle.ActiveRecord;
using DigitalFortress.Entities;

namespace DigitalFortress.Testing {
	/// <summary>
	/// Creates sample objects.
	/// </summary>
	public static class ObjectMother {

		#region Entities
		/// <summary>
		/// Returns a random integer.
		/// </summary>
		/// <returns>Returns a random integer.</returns>
		private static string GetRandomValue() {
			return Guid.NewGuid().ToString().Replace("-", string.Empty).Replace("_", string.Empty);
		}

		/// <summary>
		/// Gets a sample user's data.
		/// </summary>
		/// <returns></returns>
		private static UserEntity GetSampleUserEntity(int userIndex) {
			UserEntity u = new UserEntity();
			u.SimultaneousAccess = 1;
			u.CreateDate = DateTime.Now;
			u.IsBlocked = false;
			u.UnblockedSince = u.CreateDate;

			u.UserName = string.Format("user{0}", userIndex.ToString());

			return u;
		}

		/// <summary>
		/// Gets a sample application's data.
		/// </summary>
		/// <returns></returns>
		private static ApplicationEntity GetSampleApplicationEntity() {
			ApplicationEntity app = new ApplicationEntity();
			app.Description = "Test App Description";
			app.Password = HashGenerator.HashToString(HashGenerator.GenerateHash("TestPassword"));
			app.Name = string.Format("TestApp{0}", GetRandomValue());
			app.CreateDate = DateTime.Now;
			app.Email = string.Format("TestEmail{0}@digitalfortress.com", GetRandomValue());
			app.IsBlocked = false;
			app.UnblockedSince = app.CreateDate;

			return app;
		}

		/// <summary>
		/// Gets a sample role's data.
		/// </summary>
		/// <param name="roleName">Role name.</param>
		/// <returns>Returns the role name.</returns>
		private static RoleEntity GetSampleRoleEntity(string roleName, ApplicationEntity app) {
			RoleEntity role = new RoleEntity();
			role.RoleName = roleName;
			return role;
		}

		private static MembershipEntity GetSampleMembershipEntity(UserEntity user) {
			MembershipEntity member = new MembershipEntity();
			member.Description = "Test Membership";
			member.Email = string.Format("test{0}@tester.com", GetRandomValue());
			member.Password = HashGenerator.HashToString(HashGenerator.GenerateHash("TestPassword"));
			member.LastLoginDate = DateTime.Now;
			member.User = user;
			member.User = user;
			user.Membership = member;

			return member;
		}

		/// <summary>
		/// Initializes the database with sample data.
		/// </summary>
		/// <param name="numberOfApplications">Number of applications to create.</param>
		/// <param name="numberOfUsers">Number of users per application.</param>
		/// <param name="numberOfRoles">Number of roles per user.</param>
		public static void InitializeDatabase(int numberOfApplications, int numberOfUsers, int numberOfRoles) {
			for (int i = 0; i < numberOfApplications; i++) {
				ApplicationEntity app = GetSampleApplicationEntity();
				List<RoleEntity> roles = new List<RoleEntity>();
				for (int k = 0; k < numberOfRoles; k++) {
					RoleEntity role = GetSampleRoleEntity(string.Format("Role{0}", k.ToString()), app);
					role.Application = app;
					app.Roles.Add(role);
					roles.Add(role);
				}
				for (int j = 0; j < numberOfUsers; j++) {
					UserEntity user = GetSampleUserEntity(j);
					user.Application = app;
					MembershipEntity member = GetSampleMembershipEntity(user);
					foreach (RoleEntity role in roles) {
						user.Roles.Add(role);
					}
					app.Users.Add(user);
				}
				ActiveRecordMediator<ApplicationEntity>.CreateAndFlush(app);
			}
		}

		/// <summary>
		/// Creates an application with some roles and users.
		/// </summary>
		public static void CreateBasicApplicationData(string appName, string appDescription, 
																										string appEmail) {
			//Create DigitalFortress application.
			ApplicationEntity app1 = new ApplicationEntity();
			app1.Description = appDescription;
			app1.Email = appEmail;
			app1.Name = appName;
			app1.CreateDate = DateTime.Now;
			app1.UnblockedSince = DateTime.Now;
			app1.Password = HashGenerator.HashToString(HashGenerator.GenerateHash("dftestpassword"));
			ActiveRecordMediator<ApplicationEntity>.CreateAndFlush(app1);

			//Create role "Administrator" in DigitalFortress application.
			RoleEntity role1 = new RoleEntity();
			role1.Application = app1;
			role1.RoleName = "Administrator";
			ActiveRecordMediator<RoleEntity>.CreateAndFlush(role1);

			//Create role "User" in DigitalFortress application.
			RoleEntity role2 = new RoleEntity();
			role2.Application = app1;
			role2.RoleName = "User";
			ActiveRecordMediator<RoleEntity>.CreateAndFlush(role2);

			//Create role "Guest" in DigitalFortress application.
			RoleEntity role3 = new RoleEntity();
			role3.Application = app1;
			role3.RoleName = "Guest";
			ActiveRecordMediator<RoleEntity>.CreateAndFlush(role3);

			UserEntity user1 = new UserEntity();
			user1.UserName = "Administrator";
			user1.Application = app1;
			user1.CreateDate = DateTime.Now;
			user1.IsBlocked = false;
			user1.Roles.Add(role1);
			user1.SimultaneousAccess = 0;
			user1.UnblockedSince = user1.CreateDate;
			user1.Membership = new MembershipEntity();
			user1.Membership.Description = "Administrator User.";
			user1.Membership.Email = "heynemann@gmail.com";
			user1.Membership.Password = HashGenerator.HashToString(HashGenerator.GenerateHash("dftestpassword"));
			user1.Membership.User = user1;
			ActiveRecordMediator<UserEntity>.CreateAndFlush(user1);
		}
		#endregion Entities

		#region Contracts
		public static Application GetSampleApplication() {
			Application app = new Application();
			app.Description = "Test App Description";
			app.Name = string.Format("TestApp{0}", GetRandomValue());
			app.Email = string.Format("TestEmail{0}@digitalfortress.com", GetRandomValue());

			return app;
		}
		#endregion Contracts
	}
}
